﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ExcleToWord.DataModel;
using ExcleToWord.Util;
using ZFGK.IO;
using ZFGK.Utility;

namespace ExcleToWord
{
    public partial class MainForm : Form
    {
        private string _excelFilePath;
        private List<string> _titleColumnNameList;

        public MainForm()
        {
            InitializeComponent();
            // this.SetFormDefaultProperty();
            {
                StartPosition = FormStartPosition.CenterScreen;
                MinimizeBox = true;
                MaximizeBox = false;
                ShowInTaskbar = true;
                FormBorderStyle = FormBorderStyle.FixedSingle;
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            _excelFilePath = txtExcelPath.Text.Trim();

            //0.预处理
            if (!File.Exists(_excelFilePath))
            {
                MessageBox.Show("请先选择要处理的Excle文件", "提示");
                return;
            }
            // _titleColumnNameList = 
            _titleColumnNameList.Clear();
            foreach (ListViewItem item in listViewColumName.CheckedItems)
            {
                //item.SubItems[1].Text
                _titleColumnNameList.Add(item.Text.Trim());
            }

            if (_titleColumnNameList.Count == 0)
            {
                MessageBox.Show("请先选择Excle文件的列名", "提示");
                return;
            }

            //1.读取Excle中的信息到sheetDataList3D
            ExcelParseUtil excelUtil = new ExcelParseUtil();

            excelUtil.InitialWorkbook(_excelFilePath);
            List<RecordEntity> sheetData;
            excelUtil.ReadFirstSheet(out sheetData);

            //2.过滤
            List<Dictionary<string, string>> filteredSheetList;

            List<string> titleColumnNameListWithId = _titleColumnNameList;

            bool boolUserSelectedId = _titleColumnNameList.Contains(@"商品id"); //表示用户是否选择ID

            if (!boolUserSelectedId)
            {
                titleColumnNameListWithId.Add(@"商品id");
            }


            RecordFilterUtil.ProcessFilter(sheetData, titleColumnNameListWithId, out filteredSheetList);

            //3.输出Word
            for (int i = 0; i < filteredSheetList.Count; i++)
            {
                WordOutPutUtil.CreatNewWord(AssemblyUtil.CurrentPath(), filteredSheetList[i], i + 1, boolUserSelectedId);
            }

        }

        private void btnExcelBrowse_Click(object sender, EventArgs e)
        {
            // 提示用户选择保存位置
            OpenFileDialog dlg = new OpenFileDialog();
            // Open1.Filter = "图片文件(*.jpg,*.bmp)|*.jpg;*.gif;*.bmp"; 
            dlg.Filter = @"Excel文件(*.xls,*.xlsx,)|*.xls;*.xlsx";

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            _excelFilePath = dlg.FileName;
            txtExcelPath.Text = _excelFilePath;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
#if DEBUG
            _excelFilePath = AssemblyUtil.CurrentPath() + @"\Sample.xls";
            txtExcelPath.Text = _excelFilePath;
#endif
            _titleColumnNameList = new List<string> { @"商品名称", @"商品价格", @"平台类型", @"优惠券面额", @"券后价格", @"商品优惠券推广链接", "商品主图" };

            //更新listBOx
            {
                listViewColumName.BeginUpdate();
                listViewColumName.Items.Clear();


                listViewColumName.Scrollable = true;
                listViewColumName.CheckBoxes = true;

                foreach (var item in _titleColumnNameList)
                {
                    ListViewItem viewItem = listViewColumName.Items.Add(item);
                    viewItem.Checked = true;
                }
                listViewColumName.EndUpdate();
            }

        }

        private void btnPreViewExcleColum_Click(object sender, EventArgs e)
        {
            _excelFilePath = txtExcelPath.Text.Trim();

            //0.预处理
            if (!File.Exists(_excelFilePath))
            {
                MessageBox.Show("请先选择要处理的Excle文件", "提示");
                return;
            }

            //2.读取标题行
            ExcelParseUtil excelParseUtil = new ExcelParseUtil();
            excelParseUtil.InitialWorkbook(_excelFilePath);
            excelParseUtil.ReadFirstSheetTitleNames(out _titleColumnNameList);

            //3.填充到控件
            listViewColumName.BeginUpdate();
            listViewColumName.Items.Clear();
            foreach (var item in _titleColumnNameList)
            {
                listViewColumName.Items.Add(item);
            }

            listViewColumName.EndUpdate();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
